package com.xiaoyu.lanling.feature.visitor.controller

import `in`.srain.cube.views.list.CubeRecyclerViewAdapter
import `in`.srain.cube.views.list.CubeRecyclerViewSimpleAdapter
import `in`.srain.cube.views.list.PagedListDataModelWithRequestTag
import android.content.Context
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.xiaoyu.base.event.AppEventBus
import com.xiaoyu.base.event.SimpleEventHandler
import com.xiaoyu.base.view.list.BaseListViewController
import com.xiaoyu.lanling.event.visitor.VisitorListEvent
import com.xiaoyu.lanling.feature.visitor.model.VisitorItem
import com.xiaoyu.lanling.feature.visitor.viewholder.VisitorViewHolder
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class VisitorListViewController constructor(context: Context,
                                            listData: PagedListDataModelWithRequestTag<VisitorItem, Any>,
                                            adapter: CubeRecyclerViewAdapter<VisitorItem>,
                                            swipeRefreshLayout: SwipeRefreshLayout,
                                            recyclerView: RecyclerView
) : BaseListViewController<VisitorItem, Any>(context, listData, adapter, swipeRefreshLayout, recyclerView) {

    companion object {

        fun create(context: Context,
                   listData: PagedListDataModelWithRequestTag<VisitorItem, Any>,
                   swipeRefreshLayout: SwipeRefreshLayout,
                   recyclerView: RecyclerView): VisitorListViewController {

            val adapter = CubeRecyclerViewSimpleAdapter<VisitorItem>()
            adapter.setViewHolderClass(0, null, VisitorViewHolder::class.java, 20)

            val controller = VisitorListViewController(context, listData, adapter, swipeRefreshLayout, recyclerView)

            AppEventBus.bindContainerAndHandler(context, object : SimpleEventHandler() {

                @Subscribe(threadMode = ThreadMode.MAIN)
                fun onEvent(event: VisitorListEvent) {
                    controller.onListEvent(event)
                }

            })

            return controller
        }
    }
}